import constants as con
import pygame
import pymunk 
import math, sys, random

import xzlabel

class Brick(pygame.sprite.Sprite):
    image = None

    def __init__(self,parent):
        pygame.sprite.Sprite.__init__(self)

        if Brick.image is None:
            Brick.image = con.load_image('dat_brick.png')

        self.thud = con.load_sound('Brick.wav')

        #self.width = int(con.window_size[0] * 0.03125)
        #self.height = int(con.window_size[1] * 0.0417)
        self.width = int(con.window_size[0] * 0.05)
        self.height = int(con.window_size[1] * 0.07)

        self.parent = parent
        self.sprites = parent.sprites

        self.master = pygame.transform.scale(Brick.image, (self.width,self.height))

        need = random.randint(0,len(con.needs)-1)
        self.need = con.needs[need]

        self.coins = 0

        self.friction = 0.5
        self.inertia = 10
        self.mass = 0.25
        self.elasticity = 0

        self.centerX = int(con.window_size[0] / 2)
        self.centerY = int((con.window_size[1] / 2) * 0.7)

        w = int(self.width / 2)
        h = int(self.height / 2)
        points = [(-w, -h), (-w, h), (w,h), (w, -h)]

        self.body = pymunk.Body(self.mass, self.inertia)

        range = int(con.window_size[0] * 0.15)
        mod = random.randint(-range,range)
        self.body.position = int(con.window_size[0] * 0.4) + mod, self.centerY + (con.window_size[1] * 0.5)

        self.shape = pymunk.Poly(self.body, points, (0,0))

        self.shape.friction = self.friction
        self.shape.layers = 2 | 4
        self.shape.type = 'brick'
        self.shape.elasticity = self.elasticity
        self.shape.parent = self

        self.rect = pygame.Rect(self.body.position.x,self.body.position.y,self.width,self.height)
        self.rect.center = self.body.position

        self.sprites.add(self)
        self.label = xzlabel.Label(self)

        self.parent.space.add(self.body, self.shape)

    def update(self):
        if self.coins == 1:
            self.image = self.master
            self.image.fill((255,255,0,255), special_flags=pygame.BLEND_RGBA_MIN)
            self.image = pygame.transform.rotate(self.image, math.degrees(self.body.angle))
        elif self.coins == 2:
            self.image = self.master
            self.image.fill((255,0,0,255), special_flags=pygame.BLEND_RGBA_MIN)
            self.image = pygame.transform.rotate(self.image, math.degrees(self.body.angle))
        else:
            self.image = pygame.transform.rotate(self.master, math.degrees(self.body.angle))
        self.rect = self.image.get_rect()
        self.rect.center = con.to_pygame(self.body.position)

    def purge(self):
        self.label.purge()
        self.parent.space.remove(self.body,self.shape)
        self.parent.sprites.remove(self)
        self.parent.bricklist.remove(self)
           
    def pymunkDraw(self,screen):
        ps = self.shape.get_points()
        ps.append(ps[0])
        ps = map(con.to_pygame, ps)
        pygame.draw.lines(screen, (0,255,0), False, ps, 1)

    def play(self):
        self.thud.play()
